package org.chaos.sekiro.workorder.app.form.controller

import org.chaos.sekiro.workorder.app.form.domain.component.Ability
import org.chaos.sekiro.workorder.app.form.domain.component.Attribute
import org.chaos.sekiro.workorder.app.form.service.ComponentService
import org.chaos.sekiro.workorder.webapi.ComponentWebApi
import org.chaos.sekiro.workorder.webapi.dto.*
import org.springframework.web.bind.annotation.RestController

@RestController
class ComponentController(
    private val componentService: ComponentService
) : ComponentWebApi {
    override fun findComponent(formId: Long): List<ComponentWebDto> {
        val domains = componentService.findComponent(1L, formId)
        return domains.map {
            val attributeWebDtos = it.attributes.map { attribute: Attribute ->
                AttributeWebDto(
                    attribute.code,
                    attribute.customCode,
                    attribute.name,
                    attribute.value
                )
            }
            val abilityWebDtos = it.abilities.map { ability: Ability ->
                AbilityWebDto(
                    ability.code,
                    ability.customCode,
                    ability.name,
                    ability.value,
                    ability.enabled
                )
            }
            ComponentWebDto(
                it.id,
                it.formId,
                it.parentId,
                it.name,
                it.type,
                it.deleted,
                attributeWebDtos,
                abilityWebDtos
            )
        }
    }

    override fun findComponentAttribute(): List<ComponentAttributeWebDto> {
        val entities = componentService.findComponentAttribute(1L)
        return entities.map {
            ComponentAttributeWebDto(
                it.id,
                it.code,
                it.customCode,
                it.name,
                it.deleted
            )
        }
    }

    override fun findComponentAbility(): List<ComponentAbilityWebDto> {
        val entities = componentService.findComponentAbility(1L)
        return entities.map {
            ComponentAbilityWebDto(
                it.id,
                it.code,
                it.customCode,
                it.name,
                it.deleted
            )
        }
    }
}